package com.share.example.sbsjw.algorithm;

import com.share.common.utils.DateExtUtil;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;

@Slf4j
public class AllTableConfig {
    public static String getPhysicsTableName(String logicTableName, Object value) {
        TableConf tableConf = TableConf.toEnum(logicTableName);
        if (tableConf == null) {
            log.error("AllTableConfig配置异常logicTableName={},value={}", logicTableName, value);
            throw new RuntimeException("配置未找到");
        } else {
            String method = tableConf.method;
            int size = tableConf.size;
            switch (method) {
                case "mod":
                    return logicTableName + "_" + ((Long) value % size);
                case "yearMonth":
                    return logicTableName + "_" + DateExtUtil.getYearMonth((Date) value);
                default:
                    log.error("tableConf配置异常tableConf={},value={}", tableConf, value);
                    throw new RuntimeException("分片配置未找到");
            }
        }
    }
}

@Getter
enum TableConf {
    //定义分表数据
    st_log("st_log", -1, "yearMonth"),
    st_order("st_order", 2, "mod");

    public String code;
    public int size;
    public String method;

    TableConf(String code, int size, String method) {
        this.code = code;
        this.size = size;
        this.method = method;
    }

    public static TableConf toEnum(final String code) {
        for (final TableConf tag : TableConf.values()) {
            if (tag.code.equals(code)) {
                return tag;
            }
        }
        return null;
    }
}